<?php
class calendario
{
	var $func;
	var $request = array();

	var $anno = 0;
	var $mese = 0;
	var $gior = 0;
	
	var $mesi = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	var $ianno = 1930;
	var $imese = 1;
	var $tgiorni = 3;

	var $maxDay = 0;
	var $maxMonth = 0;
	var $maxYear = 0;

	function start()
	{
		if ( isset($this->request["maxDate"]) && $this->request["maxDate"] != "" )
		{
			$this->maxDay = intval( substr($this->request["maxDate"], 0, 2) );
			$this->maxMonth = intval( substr($this->request["maxDate"], 3, 2) );
			$this->maxYear = intval( substr($this->request["maxDate"], 6, 4) );

			$this->anno = (isset($this->request["anno"]) && $this->request["anno"] != "") ? intval($this->request["anno"]) : $this->maxYear;
			$this->mese = (isset($this->request["mese"]) && $this->request["mese"] != "") ? intval($this->request["mese"]) : $this->maxMonth;
			$this->gior = (isset($this->request["gior"]) && $this->request["gior"] != "") ? intval($this->request["gior"]) : $this->maxDay;
		}
		else
		{
			$this->anno = (isset($this->request["anno"]) && $this->request["anno"] > 0) ? intval($this->request["anno"]) : intval(date("Y"));
			$this->mese = (isset($this->request["mese"]) && $this->request["mese"] > 0) ? intval($this->request["mese"]) : intval(date("m"));
			$this->gior = (isset($this->request["gior"]) && $this->request["gior"] > 0) ? intval($this->request["gior"]) : intval(date("d"));
		}
//print_r($this->request);

		while ($this->ianno <= $this->anno) 
		{
			$this->bisestile( $this->ianno );

			$t2 = 0;
			if ($this->ianno < $this->anno)
				while($t2++ < 12) 
					$this->tgiorni = ($this->mesi[$t2] + $this->tgiorni) - (floor(($this->tgiorni + $this->mesi[$t2]) / 7) * 7);
			else
				while(++$t2 < $this->mese) 
					$this->tgiorni = ($this->mesi[$t2] + $this->tgiorni) - (floor(($this->tgiorni + $this->mesi[$t2]) / 7) * 7);
			
			$this->ianno++;
		}
	}

	function bisestile($annoBis)
	{
		if (($annoBis % 4 == 0 && $annoBis % 100 != 0) || $annoBis % 400 == 0)
		{
			$this->mesi[2] = 29;
		}
		else
		{
			$this->mesi[2] = 28;
		}
	}

	function show()
	{
		?>
		<input type="hidden" name="noAnno" value="<?php echo $this->request['noAnno'] ?>" />
		<table class="cal_table_mese_anno">
		<tr>
			<?php
				if ( $this->request['noAnno'] == '' )
				{
			?>
					<td align="right">
						<select class="cal_anno" name="c_anno" id="c_anno" width="20%" onchange="selectCalendario()">
						<?php
						if ( $this->maxYear > 0 )
							$this->ianno = $this->maxYear;
						else
							$this->ianno = date("Y") + 5;

						while($this->ianno >= 1930) 
						{
							if ($this->ianno == $this->anno)
								echo "<option value=\"".$this->ianno."\" selected>".$this->ianno."</option>\n";
							else
								echo "<option value=\"".$this->ianno."\" >".$this->ianno."</option>\n";
					
							$this->ianno--;
						}
						?>
						</select>
					</td>
			<?php
				}
			?>
			<td align="right">
				<select class="cal_mese" name="c_mese" id="c_mese" width="70%" onchange="selectCalendario()">
					<option value="1" <?php if($this->mese==1) echo "selected"?>>Gennaio</option>
					<option value="2" <?php if($this->mese==2) echo "selected"?>>Febbraio</option>
					<option value="3" <?php if($this->mese==3) echo "selected"?>>Marzo</option>
					<option value="4" <?php if($this->mese==4) echo "selected"?>>Aprile</option>
					<option value="5" <?php if($this->mese==5) echo "selected"?>>Maggio</option>
					<option value="6" <?php if($this->mese==6) echo "selected"?>>Giugno</option>
					<option value="7" <?php if($this->mese==7) echo "selected"?>>Luglio</option>
					<option value="8" <?php if($this->mese==8) echo "selected"?>>Agosto</option>
					<option value="9" <?php if($this->mese==9) echo "selected"?>>Settembre</option>
					<option value="10" <?php if($this->mese==10) echo "selected"?>>Ottobre</option>
					<option value="11" <?php if($this->mese==11) echo "selected"?>>Novembre</option>
					<option value="12" <?php if($this->mese==12) echo "selected"?>>Dicembre</option>
				</select>
			</td>
			<td onClick="closeCalendar()">X</td>
		</tr>
		</table>
		
		<table class="cal_tbl_giorni" align="center">
			<thead>
				<tr>
					<td>Lu</td>
					<td>Ma</td>
					<td>Me</td>
					<td>Gi</td>
					<td>Ve</td>
					<td>Sa</td>
					<td>Do</td>
				</tr>
			</thead>
			<tbody><?php
				$giorno = 1;
				$t6 = 0;
				while ($giorno <= $this->mesi[$this->mese]) 
				{
					$t5 = 0;
					while($t5++ < 7) 
					{
						$mid_out = "";
						$first_out = "";
						$last_out = "";
						
						if ( $this->request['noAnno'] == '' )
							$date = sprintf("%02s/%02s/%04s", $giorno, $this->mese, $this->anno);
						else
							$date = sprintf("%02s/%02s", $giorno, $this->mese);
						
						if ($t6 == 1 && $giorno <= $this->mesi[$this->mese]) 
						{
							$mid_out .= "<div style=\"width:98%;height:98%;\" onclick=\"insDate('".$date."')\"> ".$giorno." </div>";
							$giorno++;
						} 
						elseif ($t5 == $this->tgiorni && $t6==0) 
						{
							$mid_out .= "<div style=\"width:98%;height:98%;\" onclick=\"insDate('".$date."')\"> ".$giorno." </div>";
							$giorno++;
							$t6 = 1;
						} 
						elseif ($t5 == 7 && $this->tgiorni == 0 && $t6 == 0) 
						{
							$mid_out .= "<div style=\"width:98%;height:98%;\" onclick=\"insDate('".$date."')\"> ".$giorno." </div>";
							$giorno++;
							$t6 = 1;
						}

						if ($t5 == 1) $first_out .= "<tr>\n";
				
						if ( ($this->gior + 1) == $giorno && $mid_out != "")
							$first_out .= "<td class=\"cal_giorni_curr\" >\n";
						elseif ( $mid_out != "" )
							$first_out .= "<td class=\"cal_giorni\" >\n";
						else
							$first_out .= "<td class=\"cal_giorni_vuoti\" >&nbsp;\n";

						$last_out .= "</td>\n";

						if ($t5 == 7) $last_out .= "</tr>\n";

						echo $first_out.$mid_out.$last_out;
					}
				}
				?>
			</tbody>
		</table><?php
	}
}
?>